import {request} from "../../utils/util";
import { baseUrl } from '../../config'

Component({
  properties: {
    data: {
      type: Object
    },
  },
  options: {
    styleIsolation: 'apply-shared'
  },
  data: {
    openStatus: false,
    qrcodeStatus: false,
    showSelPay: false,
    payForm: {
      order_sn: '',
      pay_money: '',
    },
  },
  methods: {
    changeOpenStatus () {
      this.setData({
        openStatus: !this.data.openStatus
      })
    },
    // 取消订单
    cancelOrder() {
      wx.showModal({
        title: '提示',
        content: '确定取消当前订单吗？',
        success: (res) => {
          if (res.confirm) {
            wx.showLoading({
              title: '加载中..',
              mask: true
            })
            request({
              url: `/api/web/venue/orders/${this.data.data.id}`,
              method: 'DELETE',
              success: ({ code, message }) => {
                wx.hideLoading()
                if (code === 200) {
                  wx.showToast({
                    title: '取消成功！',
                    icon: 'success',
                    duration: 2000
                  })
                  this.triggerEvent('handle-success')
                } else {
                  wx.showToast({
                    title: message,
                    icon: 'none',
                    duration: 2000
                  })
                }
              }
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    },
    closeQrcode () {
      this.setData({
        qrcodeStatus: false
      })
    },
    showQrcode () {
      wx.showLoading({
        title: '加载中..',
        mask: true
      })
      request({
        url: `/api/web/qrcode`,
        params: {
          order_sn: this.data.data.order_sn,
          type: 'venue'
        },
        success: ({ code, data, message }) => {
          wx.hideLoading()
          if (code === 200) {
            this.setData({
              qrcodeUrl: data.url,
              qrcodeStatus: true
            })
          } else {
            wx.showToast({
              title: message,
              icon: 'none',
              duration: 2000
            })
          }
        }
      })
    },
    toComment () {
      wx.navigateTo({
        url: `/pages/makeComment/index?venue_id=${this.data.data.venue_id}&order_id=${this.data.data.id}`
      })
    },
    toPay () {
      let { order_sn, pay_money, amount } = this.data.data
      if (Number(pay_money) === 0) {
        pay_money = amount
      }
      this.setData({
        payForm: {
          order_sn,
          pay_money,
        },
        showSelPay: true
      })
    },
    closePay() {
      this.setData({
        showSelPay: false
      })
    },
    paySuccess() {
      this.triggerEvent('handle-success')
      this.closePay()
    },
    refund () {
      wx.showModal({
        title: '提示',
        content: '确认退款吗？',
        cancelColor: '#B7B7B7',
        confirmColor: '#3C9C25',
        success: (res) => {
          if (res.confirm) {
            wx.showLoading({
              title: '加载中..',
              mask: true
            })
            request({
              url: `/api/web/refund`,
              method: 'POST',
              data: {
                order_sn: this.data.data.order_sn
              },
              success: ({ code, message }) => {
                wx.hideLoading()
                if (code === 200) {
                  wx.showModal({
                    title: '提示',
                    content: '退款成功',
                    showCancel: false,
                    confirmColor: '#3C9C25',
                    confirmText: '知道了',
                    success: (res) => {
                      if (res.confirm) {
                        this.triggerEvent('handle-success')
                      }
                    }
                  })
                } else {
                  // wx.showToast({
                  //   title: message,
                  //   icon: 'none',
                  //   duration: 2000
                  // })
                  wx.showModal({
                    title: '提示',
                    content: message,
                    showCancel: false,
                    confirmColor: '#3C9C25',
                    confirmText: '确定',
                    success: (res) => {
                      if (res.confirm) {
                      }
                    }
                  })
                }
              }
            })
          }
        }
      })
    },
    // 续费
    renew() {
      let params = [
          `sport_id=${this.data.data.sport_id}`,
          `venue_id=${this.data.data.venue_id}`,
          `date=${this.data.data.order_sub[0].order_date}`,
          `from=order`
      ]
      wx.navigateTo({
        url: `/pages/reserve/index?${params.join('&')}`
      })
    },
    // 打开地图
    viewLocation() {
      const { longitude, latitude } = this.data.data;
      try {
        wx.openLocation({
          latitude: parseFloat(latitude),
          longitude: parseFloat(longitude),
          scale: 14
        });
      } catch (e) {
        wx.showToast({ title: "定位失败！" });
      }
    },
  }

})
